import axios from 'axios';
const { mobileUrl } = window.g;
const service = axios.create({
  timeout: 60000
});

// Response interceptors
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => Promise.reject(error)
);

// Response interceptors
service.interceptors.response.use(
  ({ config, data }) => {
    /* 收集待优化接口 */
    /* 对响应较慢的接口进行记录 */
    if (config.reqTime) {
      const reqDuration = new Date().getTime() - config.reqTime;
      config.reqDuration = reqDuration;
      /* 对响应时间大于5000ms的接口进行记录 */
      if (reqDuration >= 5000) {
        console.warn('以下接口响应缓慢，用时：' + reqDuration + 'ms', config.url);
      }
    }

    return data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
